/*
 * @lc app=leetcode.cn id=605 lang=cpp
 *
 * [605] 种花问题
 */

// @lc code=start
class Solution
{
public:
  bool canPlaceFlowers(vector<int>& flowerbed, int n)
  {
    int len = flowerbed.size();
    int L = 0;
    while (L < len) {
      while (L < len && flowerbed[L] == 1)
        ++L;
      int R = L;
      while (R < len && flowerbed[R] != 1)
        ++R;
      int blank = R - L;
      // -2 去除滑动窗口两边的0
      if (L - 1 >= 0)
        --blank;
      if (R < len)
        --blank;
      if (blank > 0)
        n -= (int) ceil(blank / 2.0f);
      L = R;
    }
    return n <= 0;
  }
};
// @lc code=end